﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;

namespace cutter
{
    class Program
    {


        public static float[] idlepos = new float[] { 0.090f, -0.417f, 0.339f, 1.24f, 2.83f, -0.07f };

        public static float[] idlepos2 = new float[] { -0.011f, -0.374f, 0.298f, 0.66f, 1.40f, 0.44f };

        public static float[] idlepos3 = new float[] { 0.270f, -0.195f, 0.295f, 1.20f, 2.90f, 0.00f };

        static void StartClient()
        {
            ProcessStartInfo info = new ProcessStartInfo();
            //info.FileName = "C:\\Users\\jhanbin\\Documents\\Visual Studio 2015\\Projects\\cutter\\Debug\\camera.exe";
            info.FileName = "C:\\Users\\jhanbin\\Documents\\Visual Studio 2015\\Projects\\cutter\\x64\\Debug\\camera.exe";
            info.Arguments = "";
            //info.WindowStyle = ProcessWindowStyle.Minimized;
            Process pro = Process.Start(info);
            //pro.WaitForExit();  
        }

        static void GetPath(ref List<float> p, char seg = ',', string pipename = "testpipe")
        {
            // 创建命名管道
            NamedPipeServerStream pipeServer = new NamedPipeServerStream(pipename);

            StartClient();

            //  等待客户端连接
            pipeServer.WaitForConnection();
            Console.WriteLine("Client connected.");
            try
            {
                StreamReader sr = new StreamReader(pipeServer);
                // 准备管道 reader

                string s1 = sr.ReadLine();
                Console.WriteLine("read: " + s1);

                while (true)
                {
                    int segi = s1.IndexOf(seg);
                    p.Add(Convert.ToSingle(s1.Substring(0, segi)));
                    if (segi + 1 == s1.Length)
                        break;
                    s1 = s1.Substring(segi + 1);
                }

                //Console.WriteLine("get: " + p[0] + "\t" + p[1] + "\t" + p[2] + "\t" + p[3] + "\t" + p[4] + "\t" + p[5]);
                //读取客户端回传的数据

                pipeServer.Close();
            }
            catch (IOException e)
            //异常处理
            {
                Console.WriteLine("ERROR: {0}", e.Message);
            }
        }
        
        static void Cut(bool manual = false)
        {
            UR3Control URController = new UR3Control();

            string Target_IP = "192.168.10.88";

            URController.Creat_client(Target_IP);

            URController.GoToPos(idlepos);

            return;

            List<float> p = new List<float>();

            GetPath(ref p);

            for (int i = 0; i + 5 < p.Count; i += 6)
            {
                float[] pos = new float[] { p[i], p[i + 1], p[i + 2], p[i + 3], p[i + 4], p[i + 5] };

                URController.GoToPos(pos);

                if (manual)
                {
                    string line = Console.ReadLine();
                }
            }

            URController.GoToPos(idlepos3);

            URController.Close_client();
        }
                
        static void Main(string[] args)
        {
            Cut();
        }
    }
}
